#include #include #include using namespace std; //fixed length vs. variable length records struct OfficerLocation { double latitude; double longitude; int speed; char name[100]; }; void getLocations(OfficerLocation officerLocations[], int n) { for(int i = 0; i < n; i++) { officerLocations[i].speed = rand() % 100; officerLocations[i].latitude = rand() % 90; officerLocations[i].longitude = -1 * (rand() % 90); itoa(rand() % 100000,officerLocations[i].name,2); } } void displayLocations(OfficerLocation officerLocations[], int n) { for(int i = 0; i < n; i++) { cout << officerLocations[i].speed << endl; cout << officerLocations[i].latitude << "," << officerLocations[i].longitude << endl; cout << officerLocations[i].name << endl << endl; } } void writeLocationsToFile(OfficerLocation officerLocations[], int n) { ofstream fout("locations.dat", ios::binary); fout.write((char*)officerLocations,sizeof(OfficerLocation)*n); //for(int i = 0; i < n; i++) //{ // fout.write((char*)&(officerLocations[i]),sizeof(OfficerLocation)); // //fout.write((char*)&(officerLocations[i].latitude),8); // //fout.write((char*)&(officerLocations[i].longitude),8); // //fout.write((char*)&(officerLocations[i].speed),4); // //fout.write((char*)officerLocations[i].name,100); // //fout << officerLocations[i].speed << endl; // //fout << officerLocations[i].latitude << "," << officerLocations[i].longitude << endl; // //fout << officerLocations[i].name << endl << endl; //} fout.close(); } void main() { cout << sizeof(OfficerLocation) << endl; OfficerLocation officerLocations[100]; getLocations(officerLocations,100); displayLocations(officerLocations,100); writeLocationsToFile(officerLocations,100); }